fix(parser): bind modifier rescue to whole paren-less command call - #15
Merged
Merged
Conversation
A trailing modifier `rescue` after a paren-less command call bound to the
last argument instead of the whole call: `raise "x" rescue 42` parsed as
`raise("x" rescue 42)` (so the raise ran unrescued) instead of MRI's
`(raise "x") rescue 42`. Ripper confirms `rescue_mod(command(raise, ["x"]), 42)`.
The modifier `rescue` has lower precedence than a command call's paren-less
argument list. Suppress its consumption while parsing command arguments (new
noRescueMod flag, mirroring noDo/noMasgn) so the enclosing expression wraps the
whole Call in a Begin, matching the already-correct parenthesised form
`method(args) rescue x`. The flag is cleared inside any nested delimited context
(a `(…)` group, a `(…)`/`[…]` argument list) so an inner modifier rescue there
is still consumed (`foo(bar rescue baz)`, `p ("x" rescue 42)`).
Also accept a modifier `rescue` after a `def…end` statement
(`def foo; end rescue nil`), which MRI accepts but the parser rejected, by
handling `rescue` in applyModifiers (the keyword-statement modifier path).
Unchanged: modifier rescue on a plain expression, on an assignment RHS
(`x = expr rescue default`), and the parenthesised-call form.
100% statement coverage; go vet clean.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A trailing modifier
rescueafter a paren-less command call bound to the last argument instead of the whole call:MRI parses this as
(raise "x") rescue 42(Ripper:rescue_mod(command(raise, ["x"]), 42)), so the value is42. The parenthesised formmethod(args) rescue xwas already correct.Fix
The modifier
rescuehas lower precedence than a command call's paren-less argument list. A newnoRescueModflag (mirroring the existingnoDo/noMasgn) suppresses the modifierrescuewhile parsing command arguments, so the enclosing expression wraps the wholeCallin aBegin— identical to the parenthesised form. The flag is cleared inside any nested delimited context (a(…)group, a(…)/[…]argument list), so an inner modifier rescue there is still consumed (foo(bar rescue baz),p ("x" rescue 42)).Also accepts a modifier
rescueafter adef…endstatement (def foo; end rescue nil) — MRI accepts it, the parser previously errored — by handlingrescueinapplyModifiers.AST proof
raise "x" rescue 42now yieldsBegin{ Body:[ Call{raise, ["x"]} ], Rescues:[{ Body:[42] }] }— the rescue wraps theCall, and each argument stays a plain literal.Unchanged (verified)
("x" rescue 42)x = expr rescue defaultmethod(args) rescue xVerification
MRI
ruby4.0.x match for(raise "boom" rescue 99)=>99,(raise "x","y" rescue 7)=>7,puts "a" rescue nil(runs, no rescue),def foo; end rescue nil(accepted), assignment-RHS rescue=>9.go vet ./...cleanGOWORK=off CGO_ENABLED=0🤖 Generated with Claude Code